package com.chartdemo;

import java.util.ArrayList;
import java.util.List;

import org.achartengine.GraphicalView;
import org.achartengine.chart.BarChart;
import org.achartengine.chart.BubbleChart;
import org.achartengine.chart.CombinedXYChart;
import org.achartengine.chart.CubicLineChart;
import org.achartengine.chart.LineChart;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.model.XYValueSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.graphics.Paint.Align;
import android.os.Bundle;
import android.os.Handler;

import com.chartdemo.data.DataFromJson;
import com.chartdemo.data.JsonStr;
import com.chartdemo.data.TimeStyle;

/**
 * 圆柱图Act 加载BaseChartView视图
 * 
 * @author huangtg
 */
public class ColumnChartDemo extends Activity
{
	
	private XYSeries waterSeries;
	
	private double[] mData;
	
	/**
	 * 样式
	 */
	private String[] types;
	
	/**
	 * 原始数据
	 */
	private double[] oldData = null;
	
	Handler mHand = new Handler();
	
	Runnable run = new Runnable()
	{
		
		@Override
		public void run()
		{
			if (addi < mMax + 1)
			{
				for (int i = 0; i < oldData.length; i++)
				{
					mData[i] = oldData[i] > addi ? addi : oldData[i];
				}
				addi += (int) (mMax / 25);
				ColumnChartDemo.this.init();
				mView.updateData(mChart);
				mView.invalidate();
				mHand.postDelayed(run, 50);
			}
		}
	};
	
	/**
	 * 数据中的最大值
	 */
	private double mMax;
	
	/**
	 * 自增数据与最大数据做对比
	 */
	private int addi;
	
	private int[] colors;
	
	private PointStyle[] styles;
	
	private GraphicalView mView;
	
	private XYMultipleSeriesRenderer renderer;
	
	private CombinedXYChart mChart;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		setTitle(R.string.column);
		colors = new int[] { Color.GREEN, Color.rgb(200, 150, 0) };
		styles = new PointStyle[] { PointStyle.CIRCLE, PointStyle.DIAMOND };
		addi = 0;
		mMax = 0;
		DataFromJson mJson = new DataFromJson();
		TimeStyle.TIMESTYLE = TimeStyle.YEAR;
		oldData = mJson.dealJsonData(JsonStr.yearStr, null);
		for (int i = 0; i < oldData.length; i++)
		{
			mMax = mMax > oldData[i] ? mMax : oldData[i];
		}
		
		mData = new double[oldData.length];
		this.init();
		mView = new GraphicalView(this, mChart);
		setContentView(mView);
		mHand.post(run);
	}
	
	public void init()
	{
		types = new String[] { BarChart.TYPE, BubbleChart.TYPE, LineChart.TYPE, CubicLineChart.TYPE };
		
		String[] titles = new String[] { "Crete空气温度", "Skiathos空气温度" };
		List<double[]> x = new ArrayList<double[]>();
		for (int i = 0; i < titles.length; i++)
		{
			x.add(new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 });
		}
		List<double[]> values = new ArrayList<double[]>();
		values.add(new double[] { 12.3, 12.5, 13.8, 16.8, 20.4, 24.4, 26.4, 26.1, 23.6, 20.3, 17.2, 13.9 });
		values.add(new double[] { 9, 10, 11, 15, 19, 23, 26, 25, 22, 18, 13, 10 });
		
		renderer = buildRenderer(colors, styles);
		renderer.setPointSize(5.5f);
		int mLength = renderer.getSeriesRendererCount();
		
		for (int i = 0; i < mLength; i++)
		{
			XYSeriesRenderer r = (XYSeriesRenderer) renderer.getSeriesRendererAt(i);
			r.setLineWidth(5);
			r.setFillPoints(true);
		}
		setChartSettings(renderer, "Weather data", "Month", "Temperature", 0.5, 12.5, 0, 999999,
				Color.LTGRAY, Color.LTGRAY);
		
		renderer.setXLabels(12);
		renderer.setYLabels(10);
		renderer.setShowGrid(true);
		renderer.setXLabelsAlign(Align.RIGHT);
		renderer.setYLabelsAlign(Align.RIGHT);
		renderer.setZoomButtonsVisible(true);
		renderer.setPanLimits(new double[] { -10, 20, -10, 40 });
		renderer.setZoomLimits(new double[] { -10, 20, -10, 40 });
		
		XYValueSeries sunSeries = new XYValueSeries("Sunshine hours");
		sunSeries.add(1, 35, 4.3);
		sunSeries.add(2, 35, 4.9);
		sunSeries.add(3, 35, 5.9);
		sunSeries.add(4, 35, 8.8);
		sunSeries.add(5, 35, 10.8);
		sunSeries.add(6, 35, 11.9);
		sunSeries.add(7, 35, 13.6);
		sunSeries.add(8, 35, 12.8);
		sunSeries.add(9, 35, 11.4);
		sunSeries.add(10, 35, 9.5);
		sunSeries.add(11, 35, 7.5);
		sunSeries.add(12, 35, 5.5);
		XYSeriesRenderer lightRenderer = new XYSeriesRenderer();
		lightRenderer.setColor(Color.YELLOW);
		
		waterSeries = new XYSeries("Water Temperature");
		for (int i = 0; i < mData.length; i++)
		{
			waterSeries.add(i + 1, mData[i]);
		}
		renderer.setBarSpacing(0.5);
		XYSeriesRenderer waterRenderer = new XYSeriesRenderer();
		waterRenderer.setColor(Color.BLUE);
//		XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
		XYMultipleSeriesDataset dataset = buildDataset(titles, x, values);
		dataset.addSeries(0, sunSeries);
		dataset.addSeries(0, waterSeries);
		renderer.addSeriesRenderer(0, lightRenderer);
		renderer.addSeriesRenderer(0, waterRenderer);
		waterRenderer.setDisplayChartValues(true);
		waterRenderer.setChartValuesTextSize(10);
		
		mChart = new CombinedXYChart(dataset, renderer, types);
	}
	
	/**
	 * @param colors 画笔颜色
	 * @param styles 图形样式
	 * @return XYMultipleSeriesRenderer
	 */
	private XYMultipleSeriesRenderer buildRenderer(int[] colors, PointStyle[] styles)
	{
		XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
		setRenderer(renderer, colors, styles);
		return renderer;
	}
	
	private void setRenderer(XYMultipleSeriesRenderer renderer, int[] colors, PointStyle[] styles)
	{
		renderer.setAxisTitleTextSize(16);
		renderer.setChartTitleTextSize(20);
		renderer.setLabelsTextSize(15);
		renderer.setLegendTextSize(15);
		renderer.setPointSize(5f);
		renderer.setMargins(new int[] { 20, 30, 15, 20 });
		int length = colors.length;
		for (int i = 0; i < length; i++)
		{
			XYSeriesRenderer r = new XYSeriesRenderer();
			r.setColor(colors[i]);
			r.setPointStyle(styles[i]);
			renderer.addSeriesRenderer(r);
		}
	}
	
	/**
	 * Sets a few of the series renderer settings.
	 * 
	 * @param renderer the renderer to set the properties to
	 * @param title the chart title
	 * @param xTitle the title for the X axis
	 * @param yTitle the title for the Y axis
	 * @param xMin the minimum value on the X axis
	 * @param xMax the maximum value on the X axis
	 * @param yMin the minimum value on the Y axis
	 * @param yMax the maximum value on the Y axis
	 * @param axesColor the axes color
	 * @param labelsColor the labels color
	 */
	protected void setChartSettings(XYMultipleSeriesRenderer renderer, String title, String xTitle,
			String yTitle, double xMin, double xMax, double yMin, double yMax, int axesColor, int labelsColor)
	{
		renderer.setChartTitle(title);
		renderer.setXTitle(xTitle);
		renderer.setYTitle(yTitle);
		renderer.setXAxisMin(xMin);
		renderer.setXAxisMax(xMax);
		renderer.setYAxisMin(yMin);
		renderer.setYAxisMax(yMax);
		renderer.setAxesColor(axesColor);
		renderer.setLabelsColor(labelsColor);
	}
	
	/**
	 * @param titles 图形标识
	 * @param xValues x轴值
	 * @param yValues yz轴值
	 * @return XYMultipleSeriesDataset
	 */
	protected XYMultipleSeriesDataset buildDataset(String[] titles, List<double[]> xValues,
			List<double[]> yValues)
	{
		XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
		addXYSeries(dataset, titles, xValues, yValues, 0);
		return dataset;
	}
	
	public void addXYSeries(XYMultipleSeriesDataset dataset, String[] titles, List<double[]> xValues,
			List<double[]> yValues, int scale)
	{
		int length = titles.length;
		for (int i = 0; i < length; i++)
		{
			XYSeries series = new XYSeries(titles[i], scale);
			double[] xV = xValues.get(i);
			double[] yV = yValues.get(i);
			int seriesLength = xV.length;
			for (int k = 0; k < seriesLength; k++)
			{
				series.add(xV[k], yV[k]);
			}
			dataset.addSeries(series);
		}
	}
	
	@Override
	protected void onDestroy()
	{
		mHand.removeCallbacks(run);
		super.onDestroy();
	}
}
